from django.http import JsonResponse
from calc.models import Server, Wrong
from django.http import HttpResponse
from common.page_util import page_util, page_low_util
import json
import uuid
import datetime
from common.test_port import socketTest

def test_IP(request):
    print("测试端口")
    server_list = Server.objects.all()
    wrongIP = ""
    wrongPcname = ""
    wrongQhname = ""
    wrongNum = 0
    for i in server_list:
        if(socketTest(i.ip, i.port) == False):
            wrongNum = wrongNum + 1;
            wrongIP = wrongIP + i.ip + ":" + str(i.port) + "<br/>"
            wrongPcname = wrongPcname + i.pcname + "<br/>"
            wrongQhname = wrongQhname + i.qhname + "  " + i.state + "<br/>"
    Wrong.objects.create(
        id=uuid.uuid1(),
        date=datetime.datetime.now(),
        wrongIp=wrongIP,
        wrongPcname=wrongPcname,
        wrongQhname=wrongQhname,
        wrongNum=wrongNum)
    return HttpResponse(wrongIP)


# 获取每台虚拟机信息
def server_all(request):
    pcname = request.GET['pcname']
    server_list = Server.objects.all().filter(pcname=pcname, state='1')
    result = ""
    for i in server_list:
        result = result + i.ip + ":" + str(i.port) + ":" + i.qhname + "|"
    return HttpResponse(result)


# 获取所有代理IP端口
def server_allip(request):
    server_list = Server.objects.all()
    result = ""
    for i in server_list:
        result = result + i.ip + ":" + str(i.port) + "|"
    return HttpResponse(result)



def server_page(request):
    data = json.loads(request.body)
    page = data['page']
    limit = data['limit']
    params = data['params']
    ip = params['ip']
    pcname = params['pcname']
    if ip != '':
        if pcname != '':
            server_list = Server.objects.all().filter(ip=ip, pcname=pcname)
        else:
            server_list = Server.objects.all().filter(ip=ip)
    else:
        if pcname != '':
            server_list = Server.objects.all().filter(pcname=pcname)
        else:
            server_list = Server.objects.all()
    return page_util(server_list, page, limit)


def server_info(request, id):
    server_info = Server.objects.get(id=id)
    result = json.loads(server_info.toJSON())
    return JsonResponse(result, safe=False)


def server_add(request):
    response = {}
    data = json.loads(request.body)
    server = Server.objects.create(
        id=uuid.uuid1(),
        ip=data['ip'],
        port=data['port'],
        pcname=data['pcname'],
        qhname=data['qhname'],
        state=data['state'])
    if not server is None:
        response['status'] = 200
        response['msg'] = '服务器代理信息新建成功'
        return JsonResponse(response)
    else:
        response['status'] = 400
        response['msg'] = '服务器代理信息新建失败'
        return JsonResponse(response)


# 更服务器代理信息
def server_update(request):
    response = {}
    data = json.loads(request.body)
    try:
        server = Server.objects.get(id=data['id'])
        server.ip = data['ip']
        server.port = data['port']
        server.pcname = data['pcname']
        server.qhname = data['qhname']
        server.state = data['state']
        server.save()
    except Exception:
        response['status'] = 400
        response['msg'] = '更新失败'
        return JsonResponse(response)
    response['status'] = 200
    response['msg'] = '更新成功'
    return JsonResponse(response)


# 删除服务器代理信息
def server_delete(request, id):
    response = {}
    try:
        server = Server.objects.filter(id=id)
        server.delete()
    except Exception:
        response['status'] = 400
        response['msg'] = '删除失败'
        return JsonResponse(response)
    response['status'] = 200
    response['msg'] = '删除成功'
    return JsonResponse(response)